/* @flow */

import { isUndef, isHTMLTag } from '../../../../util/index'
import { parseInputType } from './events'

//针对v-model对input, textarea, select三种标签的传值
function updateDOMProps(oldVnode, vnode) {
  if (isUndef(oldVnode.data.domProps) && isUndef(vnode.data.domProps)) {
    return
  }
  const type = (vnode.data.attrs && vnode.data.attrs.type)
  const tag = vnode.tag
  const elm = vnode.elm
  let props = vnode.data.domProps || ''

  if (tag === 'input') {
    if (parseInputType(type)) {
      if (!props) {
        elm.value = ''
      }
      if (props) {
        elm.value = props
      }
    } else if (type === 'checkbox' || type === 'radio') {
      if (!props) {
        elm.removeAttribute('checked')
      }
      if (props) {
        elm.setAttribute('checked', !!props)
      }
    }
  } else if (tag === 'textarea') {
    if (!props) {
      elm.value = ''
    }
    if (props) {
      elm.value = props
    }
  } else if (tag === 'select') {
    if (!props) {
      elm.value = ''
    }
    if (props) {
      elm.value = props
    }
  }
}



export default {
  create: updateDOMProps,
  update: updateDOMProps
}
